﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using CE.Database;
using CE.Global;
using CE.HR;
using CE.Job;
using CE.User;

public partial class HR_job_application_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.HRId] == null)
        {
            Response.Redirect("login.aspx");
        }

        if (!IsPostBack)
        {
            GetResultTable(hidJobTitle.Value, hidUser.Value, hidUserEmail.Value, hidUserContact.Value, hidVerifiedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
        }
    }
    private void GetResultTable(string searhjobtitle, string user, string useremail, string usercontact, string verifiedby, string startdate, string enddate, string status)
    {
        string _startdate = "01/01/1900";
        string _enddate = "01/01/2900";
        if (startdate != "")
            _startdate = startdate;
        if (enddate != "")
            _enddate = enddate;

        HRAccountDB _accountdb = new HRAccountDB();
        HRAccount _account = _accountdb.GetAccount(Int64.Parse(Session[CE.Global.General.HRId].ToString()));

        MyDatabase _db = new MyDatabase();
        DataSet _dsResult = new DataSet();
        string _spName = "[sprocApplicationSelectList]";
        SortedList _spara = new SortedList();
        _spara.Add("@Country", _account.Country);
        _spara.Add("@JobTitle", searhjobtitle);
        _spara.Add("@User", user);
        _spara.Add("@UserEmail", useremail);
        _spara.Add("@UserContact", usercontact);
        _spara.Add("@StartDate", DateTime.ParseExact(_startdate, "dd/MM/yyyy", null));
        _spara.Add("@EndDate", DateTime.ParseExact(_enddate, "dd/MM/yyyy", null));
        _spara.Add("@Status", status);
        _dsResult = _db.GetDataSet(_spName, _spara);

        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = _dsResult.Tables[0].DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 30;

        int curpage = 0;

        if (Request.QueryString["page"] != null)
        {
            curpage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curpage = 1;
        }

        pageds.CurrentPageIndex = curpage - 1;
        if(pageds.Count > 0)
            lblCurrpage.Text = "";
        if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: 1";
            lblCurrpageTop.Text = "Pages: 1";
        }
        else if (pageds.DataSourceCount == 0)
        {
            lblCurrpage.Text = "No data to display.";
            lblCurrpageTop.Text = "";
        }
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: <a href='job-post.aspx?" /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "&page=1'>1</a>";
            lblCurrpageTop.Text = "Pages: <a href='job-post.aspx?" /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "&page=1'>1</a>";
        }
        for (int i = 2; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
            {
                lblCurrpage.Text = lblCurrpage.Text + " " + i.ToString();
                lblCurrpageTop.Text = lblCurrpageTop.Text + " " + i.ToString();
            }
            else
            {
                lblCurrpage.Text = lblCurrpage.Text + " <a href='job-post.aspx?page=" + i.ToString() /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "'>" + i.ToString() + "</a>";
                lblCurrpageTop.Text = lblCurrpageTop.Text + " <a href='job-post.aspx?page=" + i.ToString() /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "'>" + i.ToString() + "</a>";
            }
        }

        gvApplication.DataSource = pageds;
        gvApplication.DataBind();
    }
    protected void gvApplication_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteJob")
        {
            //int _jobid = int.Parse(e.CommandArgument.ToString());
            //JobDB _jobdb = new JobDB();
            //Int64 result = _jobdb.DeleteJobByID(_jobid);
            //if (result > 0)
            //{
            //    GetResultTable();
            //}
        }
    }
    protected void gvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblRemark = (Label)e.Row.FindControl("lblRemark");
            Label lblVerifiedBy = (Label)e.Row.FindControl("lblVerifiedBy");
            Label lblRefUserID = (Label)e.Row.FindControl("lblRefUserID");

            if (lblRemark.Text == "")
                lblRemark.Text = "-";

            Int64 _hrid = Int64.Parse(lblVerifiedBy.Text);
            HRAccountDB _accountdb = new HRAccountDB();
            HRAccount _account = _accountdb.GetAccount(_hrid);

            if (_account != null)
                lblVerifiedBy.Text = _account.Name;
            else
                lblVerifiedBy.Text = "-";

            Int64 _uid = Int64.Parse(lblRefUserID.Text);
            Profile _profile = new Profile();
            ProfileDB _profiledb = new ProfileDB();
            _profile = _profiledb.GetProfile(_uid);

            if (_profile != null)
                lblRefUserID.Text = _profile.Name;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hidJobTitle.Value = txtJobTitle.Text.Trim();
        hidUser.Value = txtUser.Text.Trim();
        hidUserEmail.Value = txtUserEmail.Text.Trim();
        hidUserContact.Value = txtUserContact.Text.Trim();
        hidVerifiedBy.Value = txtVerifiedBy.Text.Trim();
        hidStartDate.Value = txtStartDate.Text.Trim();
        hidEndDate.Value = txtEndDate.Text.Trim();
        hidStatus.Value = ddlStatus.SelectedValue;
        GetResultTable(hidJobTitle.Value, hidUser.Value, hidUserEmail.Value, hidUserContact.Value, hidVerifiedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtJobTitle.Text = "";
        txtUser.Text = "";
        txtUserEmail.Text = "";
        txtUserContact.Text = "";
        txtVerifiedBy.Text = "";
        txtStartDate.Text = "";
        txtEndDate.Text = "";
        ddlStatus.SelectedIndex = 0;
        hidJobTitle.Value = "";
        hidUser.Value = "";
        hidUserEmail.Value = "";
        hidUserContact.Value = "";
        hidVerifiedBy.Value = "";
        hidStartDate.Value = "";
        hidEndDate.Value = "";
        hidStatus.Value = "all";
        GetResultTable(hidJobTitle.Value, hidUser.Value, hidUserEmail.Value, hidUserContact.Value, hidVerifiedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
    }
}